package com.grace.cms.forum.user.service.impl;

import com.grace.cms.common.enums.RecordTypeEnums;
import com.grace.cms.common.domain.entity.CmsLookRecord;
import com.grace.cms.common.enums.EventTypeEnums;
import com.grace.cms.common.event.PublicPublishEvent;
import com.grace.cms.common.model.EventModel;
import com.grace.cms.common.model.param.WebRecordParam;
import com.grace.cms.general.model.enums.CountColumnEnum;
import com.grace.cms.forum.user.mapper.HandRecordMapper;
import com.grace.cms.forum.user.model.enums.RecordTableEnum;
import com.grace.cms.forum.user.service.IHandRecordService;
import com.grace.common.core.text.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 游客模式 默认用户id为0
 */
@Service
public class HandRecordServiceImpl implements IHandRecordService {
    @Autowired
    private HandRecordMapper webRecordMapper;
    
    @Autowired
    private ApplicationContext app;

    /**
     * 发布缓存刷新事件的通用方法
     * @param recordId 记录ID
     */
    private void publishCacheRefreshEvent(String recordId,RecordTypeEnums type) {
         app.publishEvent(new PublicPublishEvent(this, 
                            new EventModel(EventTypeEnums.UPDATE, type, type.getRealId(recordId))));
        
    }
    
    @Transactional
    @Override
    public int insertLikeRecord(WebRecordParam recordParam,RecordTypeEnums type) {
        recordParam.setRecordTable(RecordTableEnum.LIKE_RECORD.getValue());
        webRecordMapper.insertRecord(recordParam);
        int result = webRecordMapper.updateCmsRecordCount(CountColumnEnum.LIKE.name().toLowerCase(),
                recordParam.getRecordId(),recordParam.getThemeName(),recordParam.getOnlyCode());
        // 使用通用方法发布缓存刷新事件
        publishCacheRefreshEvent(recordParam.getRecordId(),type);
        return result;
    }

    @Transactional
    @Override
    public int insertNastyRecord(WebRecordParam recordParam,RecordTypeEnums type) {
        recordParam.setRecordTable(RecordTableEnum.NASTY_RECORD.getValue());
        webRecordMapper.insertRecord(recordParam);
        int result = webRecordMapper.updateCmsRecordCount(CountColumnEnum.NASTY.name().toLowerCase(),
                recordParam.getRecordId(),recordParam.getThemeName(),recordParam.getOnlyCode());
        
        // 使用通用方法发布缓存刷新事件
        publishCacheRefreshEvent(recordParam.getRecordId(),type);
        
        return result;
    }

    @Transactional
    @Override
    public int insertScoreRecord(WebRecordParam recordParam,RecordTypeEnums type) {

        recordParam.setRecordTable(RecordTableEnum.SCORE_RECORD.getValue());
        webRecordMapper.insertRecord(recordParam);
        int result = webRecordMapper.updateCmsRecordCount(CountColumnEnum.SCORE.name().toLowerCase(),
                recordParam.getRecordId(),recordParam.getThemeName(),recordParam.getOnlyCode());
        
        // 使用通用方法发布缓存刷新事件
        publishCacheRefreshEvent(recordParam.getRecordId(),type);
        
        return result;
    }

    @Transactional
    @Override
    public int insertShareRecord(WebRecordParam recordParam,RecordTypeEnums type) {
        recordParam.setRecordTable(RecordTableEnum.SHARE_RECORD.getValue());
        webRecordMapper.insertRecord(recordParam);
        int result = webRecordMapper.updateCmsRecordCount(CountColumnEnum.SHARE.name().toLowerCase(),
                recordParam.getRecordId(),recordParam.getThemeName(),recordParam.getOnlyCode());
        
        // 使用通用方法发布缓存刷新事件
        publishCacheRefreshEvent(recordParam.getRecordId(),type);
        
        return result;
    }

    @Transactional
    @Override
    public int insertCollectRecord(WebRecordParam recordParam,RecordTypeEnums type) {
        recordParam.setRecordTable(RecordTableEnum.COLLECT_RECORD.getValue());
        webRecordMapper.insertRecord(recordParam);
        int result = webRecordMapper.updateCmsRecordCount(CountColumnEnum.COLLECT.name().toLowerCase(),
                recordParam.getRecordId(),recordParam.getThemeName(),recordParam.getOnlyCode());
        
        // 使用通用方法发布缓存刷新事件
        publishCacheRefreshEvent(recordParam.getRecordId(),type);
        
        return result;
    }

    @Override
    public Integer selectRecordVisible(RecordTableEnum recordTable, String recordId, String createBy) {
        return webRecordMapper.selectRecordVisible(recordTable.getValue(), recordId, createBy);
    }

    @Transactional
    @Override
    public int updateCmsRecord(WebRecordParam recordParam,RecordTypeEnums type) {
        webRecordMapper.updateCmsRecord(recordParam);
        int result = webRecordMapper.updateCmsRecordCount(Convert.toStrArray("_", recordParam.getRecordTable())[1],
                recordParam.getRecordId(),recordParam.getThemeName(),recordParam.getOnlyCode());
        // 使用通用方法发布缓存刷新事件
        publishCacheRefreshEvent(recordParam.getRecordId(),type);
        return result;
    }

    @Override
    public int updateCmsLookRecord(CmsLookRecord lookRecord) {
      return webRecordMapper.updateCmsLookRecord(lookRecord);
    }
}
